package com.vitogiovanni.tobook;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import android.app.Fragment;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.Toast;

import com.vitogiovanni.tobook.entity.localeendpoint.model.Foto;
import com.vitogiovanni.tobook.entity.localeendpoint.model.Locale;
import com.vitogiovanni.tobook.utilities.LocaleGridFotoAdapter;


	/**
	 * A placeholder fragment containing a simple view.
	 */
	public class LocaleFotoFragment extends Fragment {
		
		private LocaleGridFotoAdapter adapter;
		private Locale locale;
		private View rootView;
		private boolean noFoto = false;
		/**
		 * The fragment argument representing the section number for this
		 * fragment.
		 */
		private static final String ARG_SECTION_NUMBER = "section_number";

		/**
		 * Returns a new instance of this fragment for the given section number.
		 */
		public static LocaleFotoFragment newInstance(int sectionNumber, Locale locale) {
			LocaleFotoFragment fragment = new LocaleFotoFragment(locale);
			Bundle args = new Bundle();
			args.putInt(ARG_SECTION_NUMBER, sectionNumber);
			fragment.setArguments(args);
			return fragment;
		}

		public LocaleFotoFragment(Locale locale) {
			this.locale = locale;
		}

		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container,
				Bundle savedInstanceState) {
			rootView = inflater.inflate(R.layout.fragment_locale_foto,
					container, false);
			
			return rootView;
		}
		
		@Override
		public void onActivityCreated(Bundle savedInstanceState){
			super.onActivityCreated(savedInstanceState);
			
			List<Bitmap> listFoto = new ArrayList<Bitmap>();
			List<Foto> listFotoLocale = locale.getFoto();
			if(listFotoLocale!=null){
				for(Foto fot: listFotoLocale){
					byte[] img = fot.decodeFoto();
					ByteArrayInputStream inputStream = new ByteArrayInputStream(img);
					final BitmapFactory.Options options = new BitmapFactory.Options();
				    options.inSampleSize = 4;
				    Bitmap bitmap = BitmapFactory.decodeStream(inputStream, null, options);
					try {
						inputStream.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
					listFoto.add(bitmap);
				}
				adapter = new LocaleGridFotoAdapter(getActivity().getApplicationContext(), R.layout.grid_foto_locale, listFoto);
				
				GridView gridview = (GridView) rootView.findViewById(R.id.gridview);
				gridview.setAdapter(adapter);
				
				gridview.setOnItemClickListener(new OnItemClickListener() {
				
					@Override
					public void onItemClick(AdapterView<?> parent, View v,
						int position, long id) {
					Intent intent = new Intent(getActivity(), FullImageActivity.class);
					intent.putExtra("bitmap", adapter.getItem(position));
					startActivity(intent);		
				}
			});
			}
			else{
				noFoto = true;
			}

		}
		
		@Override
		public void setUserVisibleHint(boolean isVi){
			super.setUserVisibleHint(isVi);
			if(isVi && noFoto){
				Toast.makeText(getActivity().getApplicationContext(), getResources().getString(R.string.no_foto_locale), Toast.LENGTH_SHORT).show();
			}
		}		
		
	}